package com.baomidou.hibernate.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.hibernate.service.PointService;
import com.baomidou.hibernate.vo.PointVO;
import com.baomidou.hibernateplus.condition.SelectWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by feilin on 2017/7/28.
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Autowired
    PointService pointService;

    @RequestMapping({"","/","/json"})
    @ResponseBody
    public String index() {
        return "{id:1, text:\"中国\", loc:[333333, 4444444], time:" + new Date() + "}";
    }

    @RequestMapping(value = "/test", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String test() {
        List<PointVO> list = new ArrayList<PointVO>();
        for (int i=0; i<10; i++) {
            PointVO vo = new PointVO();
            list.add(vo);
            //vo.setId(i+2L);
            vo.setGisX(1000.3444 + i);
            vo.setGisY(2000.5555 - i);
            vo.setTime(new Date());
            vo.setTip("我是一个在途点");
        }
        boolean batchInsert = true;
        String message = null;
        try {
            batchInsert = pointService.insertBatch(list);
        } catch (Exception e) {
            message = e.getMessage();
            batchInsert = false;
        }


        if (batchInsert) {
            List<PointVO> vos = pointService.selectList(SelectWrapper.instance().le("id", 10));
            return JSONObject.toJSONString(vos);
        } else {
            return "{code: 0, message:"+ message +"}";
        }
    }
}
